package edu.zjut.hjg.saas.web.controller.tenant;

import static edu.zjut.hjg.saas.web.support.SaaSConstants.SESSION_NAME_TENANT;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import edu.zjut.hjg.saas.biz.service.ConsumptionService;
import edu.zjut.hjg.saas.common.annotation.CookieSessionIn;
import edu.zjut.hjg.saas.web.model.TenantSession;

@Controller
@RequestMapping("/tenant/consumption")
public class ConsumptionController {
    private int                pageSize = 15;

    @Autowired
    private ConsumptionService consumptionService;

    @RequestMapping("/index.htm")
    @CookieSessionIn(value = SESSION_NAME_TENANT, param = "session")
    public void index(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                      TenantSession session, Model model) {
        if (page < 1) {
            page = 1;
        }
        model.addAttribute("paginated", consumptionService.listByTenantId(session.getId(),
                pageSize, page));
    }
}
